import { observable, action } from 'mobx';
import {WeaTableNew,WeaForm} from 'comsMobx'
import { WeaTools,WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import * as Task_Apis from "../../apis/task"
import {getFormInitDatas,cptAddFormRules} from "../../util/index"
import {message,Modal} from "antd"
const confirm = Modal.confirm;
/**
 * @author ljc 2017-12-19
 * 任务的编辑，新增等
 */
export  class TaskInfoStore{
    @observable title = getLabel(15284,"编辑任务");

	@observable rightMenu = [];
	@observable taskid = "";
    @observable taskForm = new WeaForm();
    @observable visible = false;
    @observable taskInfo = {};
    @observable fieldinfo = [];
    @observable type = "";
    @observable baseParams = {}  ; //基本数据保存
    @observable validateRules = {};
    @observable selectedRowKeys = [];
   
    @action 
    handleDialog = (bool,type,id,params={} ) =>{
		this.visible = bool;
		if(bool){
            this.taskid = id;
            this.type = type;
            if(type=='add'){
                this.title= getLabel(1342,"添加任务");
            }else if(type=='view'){
                this.title= getLabel(382572,"查看任务");
            }else if(type=='edit'){
                this.title= getLabel(15284,"编辑任务");
            }
            this.baseParams =params ;
            this.getTaskInfo(type,id,params);
            if(params.callBack){
                params.callBack();
            }
		}
    }
    
    getTaskInfo=(type,id,params={})=>{
        this.taskForm = new WeaForm();
        Task_Apis.getTaskForm({viewtype:type,taskid:this.taskid,...params}).then(data=>{
            this.taskInfo = data;
            this.rightMenu = data.rightMenus;
            this.fieldinfo = data.fieldinfo;
            this.taskForm && !this.taskForm.isFormInit && this.taskForm.initFormFields(this.fieldinfo);
            this.taskForm.isFormInit && this.taskForm.updateFields(getFormInitDatas(this.fieldinfo),false);
        })
    }

    setFormFields=(value)=>{
		this.taskForm.updateFields(value, false);    //true代表完全覆盖方式更新条件值
    }

    //保存规则
	setValidate=(params={})=>{
		this.validateRules = params;
	}

    @action
    delTask=(type,taskid,params)=>{
        let _this = this;
        Modal.confirm({
            title: getLabel(15172,"系统提示"),
            content: getLabel(83925,"该任务及其子任务都会被删除，您确认要删除吗？"),
            onOk() {
                Task_Apis.delTask({method:type,taskid:taskid}).then(data=>{
                    if(data.success){
                        if(params.callBack){
                            params.callBack();
                        }
                    }else{
                        message.error(getLabel(383746,"请求失败")+"："+data.msgcode);
                    }
                })
            },
            onCancel() { },
        })
    }

    resetStage = (params,taskid) => {
        const { isFormInit } = this.taskForm;
        let conditions = this.fieldinfo ; 
        isFormInit && conditions.map(c => {
            let items = [];
            c.items.map((field, index) => {
                if(field.domkey[0]=="stageid"){
                    field.viewAttr = params.viewAttr;
                    if(params.viewAttr == "1"){
                        Task_Apis.getTaskStage({taskid: taskid }).then(data => {
                            this.taskForm.updateFields({stageid : { value : data.stageid}}, false);    //true代表完全覆盖方式更新条件值
                        });
                    }else{
                        this.taskForm.updateFields({stageid : { value : ""}}, false);
                    }
                }
            })
        })

        this.fieldinfo = conditions;
    }

    @action
    delTaskBatch=(type,taskid)=>{
        let _this = this;
        if(this.selectedRowKeys.length > 0){
            Modal.confirm({
                title: getLabel(15172,"系统提示"),
                content: getLabel(83925,"该任务及其子任务都会被删除，您确认要删除吗？"),
                onOk() {
                    Task_Apis.delTask({method:type,taskids:`${_this.selectedRowKeys}`}).then(data=>{
                        if(data.success){
                            _this.selectedRowKeys = [];
                            window._table.reLoad();
                            Modal.success({
                                title: getLabel(15172,"系统提示"),
                                content: getLabel(83472,"删除成功！"),
                            });
                        }else{
                            message.error(getLabel(383746,"请求失败")+"："+data.msgcode);
                        }
                       
                    })
                },
                onCancel() { },
            })
        } 
     
    }

    @action
    onRowSelect = (rows) => {
        this.selectedRowKeys = rows;
	}
}